﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BALayer;

namespace PGBT
{
    public partial class DanhMucNhaCungCap : Form
    {
        private NhaCungCap db = null;
        private bool flag = false;

        public DanhMucNhaCungCap()
        {
            InitializeComponent();

            db = new NhaCungCap();
        }

        private void LoadData()
        {
            dgvNCC.DataSource = db.DanhSachNhaCungCap().Tables[0];
            dgvNCC.AutoResizeColumns();
        }

        private void DanhMucNhaCungCap_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            flag = true;
            btnSave.Enabled = true;

            txtTenNCC.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtSDT.ReadOnly = false;

            btnAdd.Enabled = false;
            btnEdit.Enabled = false;
            btnDel.Enabled = false;
            btnSave.Enabled = true;

            txtId.Clear();
            txtTenNCC.Clear();
            txtDiaChi.Clear();
            txtSDT.Clear();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string error = "";

            if (!db.XoaNhaCungCap(ref error, dgvNCC.ToString()))
            {
                MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;

            txtTenNCC.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtSDT.ReadOnly = false;

            btnAdd.Enabled = false;
            btnEdit.Enabled = false;
            btnDel.Enabled = false;
            btnSave.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string error = "";
            if (flag)
            {
                if (!db.ThemNhaCungCap(ref error, txtTenNCC.Text, txtDiaChi.Text, txtSDT.Text))
                {
                    MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                if(!db.CapNhatNhaCungCap(ref error, txtId.Text, txtTenNCC.Text, txtDiaChi.Text, txtSDT.Text))
                {
                    MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            txtTenNCC.ReadOnly = true;
            txtDiaChi.ReadOnly = true;
            txtSDT.ReadOnly = true;
            btnSave.Enabled = false;

            txtId.Clear();
            txtTenNCC.Clear();
            txtDiaChi.Clear();
            txtSDT.Clear();

            LoadData();
        }

        private void dgvNCC_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvNCC.CurrentCell.RowIndex;

            txtId.Text = dgvNCC.Rows[index].Cells[0].Value.ToString();
            txtTenNCC.Text = dgvNCC.Rows[index].Cells[1].Value.ToString();
            txtDiaChi.Text = dgvNCC.Rows[index].Cells[2].Value.ToString();
            txtSDT.Text = dgvNCC.Rows[index].Cells[3].Value.ToString();
        }
    }
}
